package cn.itcast.nio.c1;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

@Slf4j(topic = "")
public class TestByteBuffer {
    public static void main(String[] args) {
        //输入流读取文件数据
        try (FileChannel channel = new FileInputStream("C:\\Users\\dell\\IdeaProjects\\Netty\\netty-demo\\data.txt").getChannel()) {
            //准备缓冲区
            ByteBuffer buffer=ByteBuffer.allocate(10);
            while(true){
                //从channer读取，向buffer写入
                int len=channel.read(buffer);
                log.debug("本次读取到的字节数{}",len);
                if(len==-1){//没有内容了；
                    break;
                }
                //切换到读模式
                buffer.flip();
                //检查是否有未读数据
                while(buffer.hasRemaining()){
                    byte b=buffer.get();
                    //打印buffer
                    log.debug("读取到的字节{}",(char)b);
                }
                //切换为写模式
                buffer.clear();
            }

        } catch (IOException e) {
        }
    }
}